package com.kanshu.ksgb.fastread.doudou.module.personal.activity

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.kanshu.ksgb.fastread.doudou.R
import com.kanshu.ksgb.fastread.doudou.app.Xutils
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants
import com.kanshu.ksgb.fastread.doudou.base.baseui.BaseActivity
import com.kanshu.ksgb.fastread.doudou.common.imageloader.CustomGlideModule
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils
import com.kanshu.ksgb.fastread.doudou.common.util.FileUtils
import com.kanshu.ksgb.fastread.doudou.common.util.StorageUtils
import com.kanshu.ksgb.fastread.doudou.common.versioncheck.VersionUpdateManager
import com.kanshu.ksgb.fastread.doudou.common.view.CustomDialog
import com.kanshu.ksgb.fastread.doudou.common.business.manager.CacheManager
import com.kanshu.ksgb.fastread.doudou.common.business.event.LoginEvent
import com.kanshu.ksgb.fastread.doudou.common.business.event.RedPointEvent
import com.kanshu.ksgb.fastread.doudou.common.business.utils.UserUtils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.layout_local_settings.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.io.File

@Route(path = ARouterConfig.PERSONAL_SETTINGS)
class SettingsActivity : BaseActivity(), View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.layout_local_settings)
        titlebarView.setTitle(getString(R.string.settings))
        account_management.setOnClickListener(this)
        clear_cache.setOnClickListener(this)
        version_update_control.setOnClickListener(this)
        about_us.setOnClickListener(this)
        showCacheSize()
        val versionUpdate = StorageUtils.getPreference(activity, Constants.SP_NAME, "version_update" + UserUtils.getUserId(), false)
        settings_red_point.visibility = if (versionUpdate) View.VISIBLE else View.GONE
        if(versionUpdate) {
            StorageUtils.setPreference(activity, Constants.SP_NAME, "version_update" + UserUtils.getUserId(), false)
            EventBus.getDefault().post(RedPointEvent())
        }
        EventBus.getDefault().register(this)
    }

    companion object {
        fun actionStart(activity: Activity) {
            val intent = Intent(activity, SettingsActivity::class.java)
            activity.startActivity(intent)
        }
    }

    override fun onClick(v: View?) {
        if (v == null) {
            return
        }
        when (v.id) {
            R.id.account_management ->{
                ARouterUtils.toActivity(ARouterConfig.PERSONAL_ACCOUNT_MANAGE)
            }
            R.id.clear_cache -> {
                clearCache(this)
            }
            R.id.version_update_control -> {
                VersionUpdateManager.getInstance().checkVersionFromNet(this,false, true)
            }
            R.id.about_us -> {
                ARouterUtils.toActivity(ARouterConfig.PERSONAL_ABOUT_US)
            }
        }
    }

    /**
     * 清除缓存
     *
     * @param context
     */
    private fun clearCache(context: Context) {
        CustomDialog.show(this, "提示", "确定清除缓存吗?", object : CustomDialog.Callback{
            override fun onSure(dialog: Dialog?) {
                // 图片缓存
                GlideImageLoader.clearDiskCache(context);
                //CacheManager.getInstance().clearCache();
                // 删除缓存章节文件
                FileUtils.deleteFolderFile(CacheManager.getInstance().getPathDataTxt(), true);
                val formatSize = FileUtils.getFormatSize(0.toDouble())
                cache_size.text = formatSize;
                dialog?.dismiss()
            }
            override fun onCancel(dialog: Dialog?) {
                dialog?.dismiss()
            }
        }, false);
    }

    private fun showCacheSize() {
        // 格式化缓存大小
        Schedulers.io().scheduleDirect {
            val cacheFile = File(FileUtils.getCacheDir(Xutils.getContext()), CustomGlideModule.CACHE_NAME);
            // 图片缓存目录大小
            val folderSize = FileUtils.getFolderSize(cacheFile);
            val chapterFiles = File(CacheManager.getInstance().getPathDataTxt());
            val cacheSize = FileUtils.getFormatSize((FileUtils.getFolderSize(chapterFiles) + folderSize).toDouble());
            AndroidSchedulers.mainThread().scheduleDirect {
                if (cache_size != null) {
                    cache_size.setText(cacheSize);
                }
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun handleLoginEvent(event: LoginEvent) {
        finish()
    }

    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
    }
}